﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Security.Cryptography;

public partial class _Default : System.Web.UI.MasterPage
{
    /// <summary>
    /// 页面载入事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        this.WebPartManager1.DisplayMode = WebPartManager.BrowseDisplayMode;//设置webPart显示模式
        if (Session["UserID"] != null)//如果用户已登陆
        {
            this.Label1.Text = Session["UserName"].ToString();//显示用户名
            LinkButton1.Text = "点击这里注销";//改变登陆按钮状态
        }
    }

    /// <summary>
    /// 主题列表更改
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ddlThemes_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["myTheme"] = ddlThemes.SelectedValue.ToString();//将设置的主题保存至会话中
        Response.Write("<script>alert('" + "主题设置成功，请刷新页面" + "');</script>");//弹出信息
    }

    /// <summary>
    /// 显示登陆窗按钮点击
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        if (Session["UserID"]!=null)//如果用户已经登陆
        {
            Session["UserID"] = null;//注销用户
            Response.Write("<script>alert('" + "退出成功" + "');</script>");//弹出提示
            LinkButton1.Text = "点击这里登陆";//改变自身状态
            Response.Redirect("Default.aspx");//页面转到首页
        }
        else//若用户尚未登陆
        {
            this.Panel1.Visible = true;//显示登陆窗
        }
    }

    /// <summary>
    /// 登陆按钮点击
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Login_Click(object sender, EventArgs e)
    {
        MD5 pw=new MD5();//生成MD5对象
        byte[] password= pw.GetMD5(txtPassword.Text);//加密密码
        string strUserName = BLL.CallProc.UserLogin(txtUserName.Text, password);//尝试登陆用户
        if (strUserName != null)//若登陆成功
        {
            Session["UserID"] = txtUserName.Text;//设置会话中的用户ID
            Session["UserName"] = strUserName;//设置会话中的用户名
            Response.Write("<script>alert('" + "登陆成功，欢迎你:" +Session["UserName"]+ "');</script>");//弹出成功信息
            this.Label1.Text = strUserName;//显示用户名
            this.Panel1.Visible = false;//隐藏登陆框
            LinkButton1.Text = "点击这里注销";//改变登陆按钮状态

            //页面转到我的信息
            this.Response.Output.WriteLine("<script language=\"javascript\">");
            this.Response.Output.WriteLine("parent.window.location=\"" + this.Request.ApplicationPath + "/MyProfile.aspx\"");
            this.Response.Output.WriteLine("</script>");
        }
        else//若登陆失败
        {
            this.Label1.Text = "错误！";//显示错误信息
            Response.Write("<script>alert('" + "登陆失败，请重新登陆" + "');</script>");//弹出错误信息
        }
    }
}
